package com.ahcloud.edas.gatewaylog.biz.infrastructure.sharding.algorithm;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingAlgorithm;
import org.apache.shardingsphere.api.sharding.standard.PreciseShardingValue;

import java.util.Collection;

/**
 * @program: ahcloud-operatelog
 * @description:
 * @author: YuKai Fan
 * @create: 2024/6/6 13:16
 **/
@Slf4j
public class GatewayLogDbShardingAlgorithm implements PreciseShardingAlgorithm<String> {
    private final static String ACTUAL_DATA_PREFIX = "log";

    @Override
    public String doSharding(Collection<String> availableTargetNames, PreciseShardingValue<String> shardingValue) {
        int hash = (shardingValue.getValue().hashCode() & Integer.MAX_VALUE) % 4;
        return ACTUAL_DATA_PREFIX + hash;
    }
}
